package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116668475";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDabAF5ukwDvepMgDgJUZhGlSRHymOVu9hyuVDU9W0rVIX3TXa2gcClAT3lMu/3TfveR91NirJ2TVybyUPeH1HrqUPOtso2pmzHOwAK54Z2e3Mr8OADrv4LK/RLaUAU1zPOfu/zih42L9oh/HE1zjtiZwB/VEg6PbtntOg2+xQDG6IWOcHNyeefHO/HKKBbjzjTOUvc0re1p+dFMMMklrriICYNfOIC0Gxo9iHtyJGI2CYtt0jx8EDr1M07M68m+ovD4xsJB/3/yt1jVVK4uTWh8nl9Hab7l3Xsy2oiYorGBQUvYm5g1mJfV5shBHa+zMmYB2FqF1DlsFZBeCIL0fDtAgMBAAECggEAJUcDpXPPH2BdSXsklSqQ05MGox3+iwKJOWM5UW8ll5yQVJmq5hEVANRKbQCZhdqQXupi8aX3OUuQv6HE7Hbp809rHC+EKXmT3RjLtBC5mSUOjfRJNze5ccypn+kxn5zu4ZI2f5kV+OeH0f8UjztIVU4A/royrYX1owh0DVYinzjaY64UNiS6dqgENfQNRoYbcAl3XfUq37DxSCscUkD6C7LhFknJWRKFbzQn1Paft6Myq9QFg+032aJbQK87Juwcul3W+w2uZtfjdWDdBFaGg6OuCDEh8nvlb6njn78ih4/4Nq6mpStsBDKl6h3JL35ji3/UZxA+2Eus93HsQFwHQQKBgQDvEGtUTvGf+mGPuPyTRNxC/ZxYV54wjoQNbNpPPJNpzHdrfK9HyODeG/ChadGe24JUF4u1wKBlZHa/TO3Oq7Q+XmDVS0CSV8CO4JY9/xD315h030BluhnPAdbPHn0URNCdpRwOLzEvtitPOP2E0J90nIKiOpureVa5s3i8QyzqfQKBgQDp5TnvJlbQBecMVR5EdsX1QklbE/GzDg8pN6gJWvLagqcKO/YB+twohm732YgcEzvQI/qEwGmchaVl9M7LoJNfoIvgmkPOJ0ot+ybYXZEyXZt2NjOhWPovtJEPVt7AHmjYdB9j7cIxRfwpMOLGgX4f//NAWACgWkeXxLO27/t7MQKBgHIlaNnsh7pXFtZFAXRf4Lur1EbqLwIISk0OuH408j4F/s83zYl9BMGInbC0kf5D2UrFo1O5EdzXx0s4NCQGIB3XemzdbYtykuKloZcgfr2YF5dWMa1g2onY2vQyHo7a/LEI8QhkyBt749oD4CjWZAyN3mIPkdyiJyKr6u4ZNQ0RAoGAQo9i7zV/2u7zE0Vduan7J5JXgb2pOIU1t0sJXLAvEBPCKV1q4qFa6YVoKswSubqXeOHkNsbyxwIZUWxbNQNbLQlpBN+hkBcKOw+PvF7UJPsSdI90HeROPikqZq0X8qA0wfQ5wpw0eQUrfyVUQ50yITUbKiJg+0D1wrDNW6/rZuECgYEAwkw5BAWZS3xj9jXXJVIijY6q73jblaAUs96KmyxlIVdxXuP+ihZ/9Zs2xtQBFVko+I5Z1aCT7RfGsEODWy2fsm+U8vYjxomrH6zcTnclKw8K3q9m312l5egbau/Iep6AGplOao+MdXm56WeJ78DfozCOdwa8UnPza/h88dtJi7E=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjrEVFMOSiNJXaRNKicQuQdsREraftDA9Tua3WNZwcpeXeh8Wrt+V9JilLqSa7N7sVqwpvv8zWChgXhX/A96hEg97Oxe6GKUmzaZRNh0cZZ88vpkn5tlgL4mH/dhSr3Ip00kvM4rHq9PwuT4k7z1DpZAf1eghK8Q5BgxL88d0X07m9X96Ijd0yMkXArzD7jg+noqfbztEKoH3kPMRJC2w4ByVdweWUT2PwrlATpZZtYLmtDvUKG/sOkNAIKEMg3Rut1oKWpjyYanzDgS7Cg3awr1KPTl9rHCazk15aNYowmYtVabKwbGVToCAGK+qQ1gT3ELhkGnf3+h53fukNqRH+wIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息

    public String notify_url = "http://361i003r20.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页

    public String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
